package com.guigu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.guigu.ShoppingCartService;
import com.guigu.pojo.FrontShoppingCart;
import com.guigu.vo.Pojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("ShoppingCart")
@RestController
@CrossOrigin
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    //查询购物车列表
    @GetMapping("All/{pageNo}/{pageSize}/{uid}")
    private Pojo<FrontShoppingCart> All(@PathVariable int pageNo,@PathVariable int pageSize,@PathVariable int uid){
        PageInfo<FrontShoppingCart> frontShoppingCartPageInfo = shoppingCartService.selectPage(pageNo, pageSize,uid);
        Pojo<FrontShoppingCart> objectPojo = new Pojo<>();
        objectPojo.setTotal(frontShoppingCartPageInfo.getTotal());
        objectPojo.setRows(frontShoppingCartPageInfo.getList());
        return objectPojo;
    }
    //删除购物车商品
    @DeleteMapping("/{id}")
    private String delete(@PathVariable int id){
        boolean b = shoppingCartService.removeById(id);
        if(b)
            return "success";
        return "error";
    }
    //修改购物车商品数量
   @PutMapping("/{cid}/{num}")
   private String update(@PathVariable int cid,@PathVariable int num){
       boolean b = shoppingCartService.updateShoppingCart(cid, num);
       if(b)
           return "success";
       return "error";
   }
    /**
     * 添加购物项
     * @param
     * @param
     * @return
     */
    @PostMapping("/{uid}/{cid}/{num}")
    private String insert(@PathVariable int uid,@PathVariable int cid,@PathVariable int num){
        boolean b = shoppingCartService.InsertShoppingCart(uid, cid, num);
        if(b)
            return "success";
        return "error";
    }
            //xiaosong
    //根据userId删除
    @DeleteMapping("/deleteById/{userId}")
    public String deleteById(@PathVariable int userId) {
        System.out.println("shoppingCart"+userId);
        QueryWrapper<FrontShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        boolean b = shoppingCartService.remove(queryWrapper);
        if (b)
            return "ok";
        return "no";
    }
}
